/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yufei.mail.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.yufei.mail.entity.Email;
import com.yufei.mail.entity.MailSenderInfo;
import com.yufei.mail.service.MailService;
import com.yufei.mail.service.MailServiceImpl;
import com.yufei.ui.utils.ComponentUtils;
import com.yufei.ui.utils.DialogUtil;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.FileUtil;


/**
 *
 * @author jasstion
 */
public class BatchEmailSenderClient extends javax.swing.JFrame {
	private static Log mLog = LogFactory
			.getLog(BatchEmailSenderClient.class);
    /**
     * Creates new form BatchEmailSenderClient
     */
    public BatchEmailSenderClient() {
        initComponents();
        this.setLayout(new FlowLayout());
        excelFilePath.setPreferredSize(new Dimension(200, 30));
        this.getContentPane().add(jLabel1);
        this.getContentPane().add(excelFilePath);
        this.getContentPane().add(sendMailButton);
        
        ComponentUtils.centerComponent(this);
        pack();
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        excelFilePath = new javax.swing.JTextField();
        sendMailButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Excel文件路径");

        sendMailButton.setText("发送邮件");
        sendMailButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMailButtonActionPerformed(evt);
            }
        });

       


      
    }// </editor-fold>//GEN-END:initComponents

    private void sendMailButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendMailButtonActionPerformed
        // TODO add your handling code here:
    	List<List<String>> strs=null;
    	List<Email> emails=null;
    	try {
    		strs=FileUtil.parseExcelFile(excelFilePath.getText());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			DialogUtil.popUpDialog("请检查Excel格式是否正确！");
			return;
		}
    	String beanName="com.yufei.mail.entity.Email";
    	List<String> fieldOrders=new ArrayList<String>();
    	fieldOrders.add("title");

    	fieldOrders.add("content");

    	fieldOrders.add("getEmailAdress");
		emails=FileUtil.listToBeans(strs, beanName, fieldOrders);
		MailService mailService=new MailServiceImpl();
		MailSenderInfo  mailSenderInfo=(MailSenderInfo) CommonUtil.getObjectFromXml(Thread.currentThread().getContextClassLoader().getResourceAsStream("mailSenderInfo.xml"), MailSenderInfo.class);
		for(Email email:emails){
			mailService.sendEmail(email, mailSenderInfo);
		}
    }//GEN-LAST:event_sendMailButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BatchEmailSenderClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BatchEmailSenderClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BatchEmailSenderClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BatchEmailSenderClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
      final  BatchEmailSenderClient batchEmailSenderClient = new BatchEmailSenderClient();
   

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
              
				batchEmailSenderClient.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField excelFilePath;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton sendMailButton;
    // End of variables declaration//GEN-END:variables
}
